iT邦幫忙

2023 iThome 鐵人賽

DAY 11
0
自我挑戰組

Leetcode Top Interview 150系列 第 11

58. Length of Last Word

  • 分享至 

  • xImage
  •  

Given a string s consisting of words and spaces, return the length of the last word in the string.

A word is a maximal
substring
consisting of non-space characters only.

Example 1:

Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Example 2:

Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
Example 3:

Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.

Constraints:

1 <= s.length <= 104
s consists of only English letters and spaces ' '.
There will be at least one word in s.

# @param {String} s
# @return {Integer}
def length_of_last_word(s)
    s.split(' ').last.length
end

上一篇
13. Roman to Integer
下一篇
28. Find the Index of the First Occurrence in a String
系列文
Leetcode Top Interview 15015
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言